package com.yzjs.localhost.service.hrmTitleChange.impl;

import com.alibaba.fastjson.JSONObject;
import com.yzjs.common.base.service.BaseServiceImpl;
import com.yzjs.common.util.conversion.JsonUtil;
import com.yzjs.localhost.dao.hrmTitleChange.HrmTitleChangeMapper;
import com.yzjs.localhost.dto.ComparisonResultDTO;
import com.yzjs.localhost.dto.DataBundlingDTO;
import com.yzjs.localhost.dto.UserRightsDTO;
import com.yzjs.localhost.entity.hrmProfessionalQualification.HrmProfessionalQualificationEntity;
import com.yzjs.localhost.entity.hrmTitleChange.HrmTitleChangeEntity;
import com.yzjs.localhost.entity.tDataPersistenceAfter.TDataPersistenceAfterEntity;
import com.yzjs.localhost.entity.tDataPersistenceBeforce.TDataPersistenceBeforceEntity;
import com.yzjs.localhost.service.AnalysisService;
import com.yzjs.localhost.service.hrmTitleChange.HrmTitleChangeService;
import com.yzjs.localhost.service.tDataPersistenceAfter.TDataPersistenceAfterService;
import com.yzjs.localhost.service.tDataPersistenceBeforce.TDataPersistenceBeforceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;


@Service("hrmTitleChangeService")
public class HrmTitleChangeServiceImpl extends BaseServiceImpl<HrmTitleChangeEntity, HrmTitleChangeMapper> implements HrmTitleChangeService {

    @Autowired
    private TDataPersistenceBeforceService dataPersistenceBeforceService;

    @Autowired
    private TDataPersistenceAfterService dataPersistenceAfterService;


    @Override
    public ComparisonResultDTO TitleChangeCompliance(DataBundlingDTO dto, UserRightsDTO rights) throws Exception {
        TDataPersistenceBeforceEntity before = dto.getBeforce();
        String beforeInfo = before.getInfo();
        Map<String, Object> beforeParse = JsonUtil.json2map(beforeInfo);
        TDataPersistenceAfterEntity after = dto.getAfter();
        String afterInfo = after.getInfo();

        Map<String, Object> afterParseMap = JsonUtil.json2map(after.getInfo());
        Map<String, Object> differenceMap = new HashMap<>();
        ComparisonResultDTO resultDTO = null;

        if (before.getOperatorCode().contains("add")) {
            Map<String, Object> afterParse = JsonUtil.json2map(afterInfo);
            // 比对 原工作单位,用户ID,变动后工作单位,原职称ID,变动原因,toDate,更新人
            resultDTO = AnalysisService.comparisonMap(before.getOperatorCode(), beforeParse, afterParse, "tcYdw,tcUser,tcBddw,tcZcid,tcReason,tcDate,updateBy");
        } else if (before.getOperatorCode().contains("edit")) {
            Map<String, Object> afterParse = JsonUtil.json2map(afterInfo);
            // 比对 部门名称,staffNum,原工作单位,用户ID,主键ID,原职称ID,变动后工作单位,变动后职称ID,变动原因,fullName,tcDate,创建时间,更新人,是否删除系统字典表
            resultDTO = AnalysisService.comparisonMap(before.getOperatorCode(), beforeParse, afterParse, "deptName,staffNum,tcYdw,tcUser,tcId,tcZcid,tcBddw,tcBdid,tcReason,fullName,tcDate,createDate,updateBy,deleteFlag");
        } else if (before.getOperatorCode().contains("remove")) {
            beforeParse.put("deleteFlag", "2");
            // 比对 职称变动记录ID
            resultDTO = AnalysisService.comparisonMap(before.getOperatorCode(), beforeParse, afterParseMap, "tcIds,deleteFlag");
            Object tcId =  afterParseMap.get("tcIds");
            if (tcId == null) {
                resultDTO.setAbstractName("主键ID不存在");
                resultDTO.setWarningInfo("主键ID不存在");
                resultDTO.setMathFlag(false);
                return resultDTO;
            }
            HrmTitleChangeEntity titleChangeEntity = new HrmTitleChangeEntity();
            titleChangeEntity.setTcId(Long.valueOf((String) tcId));
            titleChangeEntity.setDeleteFlag((String) afterParseMap.get("deleteFlag"));
            titleChangeEntity.setDataIdentification(after.getDataIdentification());
            this.insert(titleChangeEntity);
            return resultDTO;
        }
        // 记录表信息
        HrmTitleChangeEntity hrmTitleChangeEntity = JSONObject.parseObject(afterInfo, HrmTitleChangeEntity.class);
        hrmTitleChangeEntity.setPermissionMirroring(JsonUtil.obj2jsonIgnoreNull(rights));
        hrmTitleChangeEntity.setDataIdentification(after.getDataIdentification());
        this.insert(hrmTitleChangeEntity);

        return resultDTO;
    }

}
